# coding=utf8
# import os
import time
from myself_robot.handle.handle_data import Handle
from myself_robot.mytools.tools import *


class Creat_report:
    def __init__(self):
        self.tempreport = pro_path() + r"\report\temp_report.html"

    def creat_report(self, data, filename=pro_path() + r"\report\report.html"):
        with open(self.tempreport, "r", encoding="utf8") as f:
            content = f.read()
            suc, fail, skip, total = self.__list(data)
            content = content.replace("&version", "V 1.0")
            content = content.replace("&total", str(total))
            content = content.replace("&suc", str(suc))
            content = content.replace("&fail", str(fail))
            content = content.replace("&skip", str(skip))
            content = content.replace("&time", str(time.strftime('%Y-%m-%d %H:%M:%S')))
            add = ''
            j = 0
            n = 0
            for i in Handle().handle():
                ii = data[n]
                if len(ii) == 2:
                    add += "<tr>" \
                           f"<td>{i.id}</td><td>{i.moudle}</td><td>{i.disc}</td><td>{i.report}</td><td>{i.exp}</td><td>{data[j][0]}</td><td><a href={ii[1]}>图片</a></td>" \
                           f"</tr>"
                else:
                    add += "<tr>" \
                           f"<td>{i.id}</td><td>{i.moudle}</td><td>{i.disc}</td><td>{i.report}</td><td>{i.exp}</td><td>{data[j][0]}</td><td>NULL</td>" \
                           f"</tr>"
                j += 1
                n += 1
            content = content.replace("&content", add)
        with open(filename, "w", encoding="utf8") as f:
            f.write(content)

    def __list(self, data):
        suc, fail, skip, total = 0, 0, 0, 0
        for info in data:
            if info[0] == 'pass':
                suc += 1
            elif info[0] == 'fail':
                fail += 1
            elif info[0] == 'skip':
                skip += 1
        total = suc + fail + skip
        return suc, fail, skip, total


if __name__ == '__main__':
    a = Creat_report()
    a.creat_report(['pass', 'pass', 'fail'])
